import request from '@/utils/request'

//  班级分页
export function getClassList(params) {
  return request({
    url: '/base/class/listPage',
    method: 'get',
    params
  })
}

// 更新班级
export function updateClassField(params) {
  return request({
    url: '/base/class/updateById',
    method: 'put',
    data: params
  })
}

// 批量删除 逗号隔开的id字符串
export function bathDeleteClass(ids) {
  return request({
    url: `/base/class/deleteByIds/${ids}`,
    method: 'delete'
  })
}

// 新增班级
export function addClass(data) {
  return request({
    url: `/base/class/add`,
    method: 'put',
    data
  })
}

// 通过学院代码 获取专业信息
export function getMajorByCollegeCode(collegeCode) {
  return request({
    url: `/base/class/getMajorByCollegeCode?collegeCode=${collegeCode}`,
    method: 'get'
  }).then(res => {
    if (res.status) {
      return res.data.map(d => ({
        name: d.majorName,
        value: d.majorCode
      }))
    } else {
      return []
    }
  })
}

// 获取年份
export function getTermYear() {
  const nf = new Date().getFullYear()
  const arr = []
  // console.log(nf)
  for (let i = Number(nf) + 3; i >= 1991; i--) {
    arr.push(
      { value: i }
    )
  }
  return arr
}

// 生成班级代码
export function createCode(majorCode, grade) {
  return request({
    url: `/base/class/getClassCode`,
    method: 'get',
    params: {
      majorCode: majorCode,
      grade: grade
    }
  })
}

// 验证班级代码是否重复
export function testRepeat(classCode) {
  return request({
    url: `/base/class/testRepeat`,
    method: 'get',
    params: {
      classCode: classCode
    }
  })
}

// 批量更新
export function updateByIds(form) {
  form.checkCreate = 1
  return request({
    url: `/base/class/updateByIds/${form.tids.join(',')}`,
    method: 'put',
    data: form
  })
}

// 查询班级
export function getPublicClassList(params) {
  return request({
    url: `/base/class/name`,
    method: 'get',
    params: { majorCode: params.majorCode, grade: params.grade }
  }).then(res => {
    if (res.status) {
      return res.data.map(d => ({
        name: d.className,
        value: d.classCode
      }))
    } else {
      return []
    }
  })
}
